{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "outputs": [],
      "source": "import numpy as np",
      "metadata": {
        "pycharm": {
          "metadata": false,
          "name": "#%%\n"
        }
      }
    },
    {
      "cell_type": "markdown",
      "source": "* 1 将建布尔数组",
      "metadata": {
        "pycharm": {
          "metadata": false
        }
      }
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "outputs": [],
      "source": "v1 \u003d np.full((3,3), True, dtype\u003dbool)\nprint(v1)\n",
      "metadata": {
        "pycharm": {
          "metadata": false,
          "name": "#%%\n"
        }
      }
    },
    {
      "cell_type": "markdown",
      "source": "* 2 从一维数组中提取指定元素",
      "metadata": {
        "pycharm": {
          "metadata": false
        }
      }
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "outputs": [],
      "source": "v2 \u003d np.arange(1,10,dtype\u003dint)\nprint(v2)\nv3 \u003d v2[v2 %2 \u003d\u003d1]\nprint(v3)\n",
      "metadata": {
        "pycharm": {
          "metadata": false,
          "name": "#%%\n"
        }
      }
    },
    {
      "cell_type": "markdown",
      "source": "* 3 用另一个值替换满足条件的元素项目",
      "metadata": {
        "pycharm": {
          "metadata": false
        }
      }
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "outputs": [],
      "source": "v4 \u003d np.arange(1,10,dtype\u003dint)\nprint(v4)\nv4[v4 % 2 \u003d\u003d 1] \u003d -2\nprint(v4)\nv5 \u003d v4\nprint(v5)\n",
      "metadata": {
        "pycharm": {
          "metadata": false,
          "name": "#%%\n"
        }
      }
    },
    {
      "cell_type": "markdown",
      "source": "* 4 获取两个numpy数组之间的公共项",
      "metadata": {
        "pycharm": {
          "metadata": false
        }
      }
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "outputs": [],
      "source": "v7 \u003d np.arange(1, 6)\nv8 \u003d np.arange(4,12)\nprint(v7,v8)\nnp.intersect1d(v7, v8)\n",
      "metadata": {
        "pycharm": {
          "metadata": false,
          "name": "#%%\n"
        }
      }
    },
    {
      "cell_type": "markdown",
      "source": "* 5 从一个数组删除存在于另一个数组中的元素项",
      "metadata": {
        "pycharm": {
          "metadata": false
        }
      }
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "outputs": [],
      "source": "np.setdiff1d(v7,v8)\nnp.setdiff1d(v8, v7)\n",
      "metadata": {
        "pycharm": {
          "metadata": false,
          "name": "#%%\n"
        }
      }
    },
    {
      "cell_type": "markdown",
      "source": "* 6 获取两个数组元素匹配的位置",
      "metadata": {
        "pycharm": {
          "metadata": false
        }
      }
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "outputs": [],
      "source": "import numpy as  np\na1 \u003d np.arange(1,10)\na2 \u003d np.arange(5,10)\nprint(a1, a2)\nnp.where(a1 \u003d\u003d a2)\n",
      "metadata": {
        "pycharm": {
          "metadata": false,
          "name": "#%%\n"
        }
      }
    },
    {
      "cell_type": "markdown",
      "source": "* 7 交换数组的两列",
      "metadata": {
        "pycharm": {
          "metadata": false
        }
      }
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "outputs": [],
      "source": "a3 \u003d np.arange(9).reshape(3,3)\nprint(a3)\na3[:, [1,0,2]]\na3[:, [1,2,0]]\n",
      "metadata": {
        "pycharm": {
          "metadata": false,
          "name": "#%%\n"
        }
      }
    },
    {
      "cell_type": "markdown",
      "source": "* 8 交换数组两行",
      "metadata": {
        "pycharm": {
          "metadata": false
        }
      }
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "outputs": [],
      "source": "a4 \u003d np.arange(9).reshape(3,3)\na4[[1,0,2], :]\n",
      "metadata": {
        "pycharm": {
          "metadata": false,
          "name": "#%% \n"
        }
      }
    },
    {
      "cell_type": "markdown",
      "source": "* 9 从np数组中提取指定范围的元素\n* * 使用索引找出位置\n* * 根据索引拿到数组",
      "metadata": {
        "pycharm": {
          "metadata": false
        }
      }
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "outputs": [],
      "source": "a6 \u003d np.arange(15)\na6\nindex \u003d np.where((a6\u003e5) \u0026 (a6\u003c10))\na6[index]\n",
      "metadata": {
        "pycharm": {
          "metadata": false,
          "name": "#%%\n"
        }
      }
    },
    {
      "cell_type": "markdown",
      "source": "* 10 创建包含5-10大小元素的随机二维数组\n* * 创建随机的5x3的二维数组",
      "metadata": {
        "pycharm": {
          "metadata": false
        }
      }
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "outputs": [],
      "source": "# 方法1\nrand_arr1 \u003d np.random.randint(low\u003d5, high\u003d10, size\u003d(5,3))\nrand_arr1\n# 方法2\nrand_arr3 \u003d np.random.randint(low\u003d5, high\u003d10, size\u003d(5,3)) + np.random.rand(5,3)\nrand_arr3\n# 方法3\nrand_arr4 \u003d np.random.uniform(5,10, size\u003d(5,3))\nprint(rand_arr4)\n",
      "metadata": {
        "pycharm": {
          "metadata": false,
          "name": "#%%\n"
        }
      }
    },
    {
      "cell_type": "markdown",
      "source": "* 11 数组中只输出小数点后3位",
      "metadata": {
        "pycharm": {
          "metadata": false
        }
      }
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "outputs": [],
      "source": "rand_arr5 \u003d np.random.random([5, 3])\nrand_arr5\nnp.set_printoptions(precision\u003d3) # 限制输出位数\n# np.set_printoptions(thershold\u003d6) #限制输出结果个数\nrand_arr5\n",
      "metadata": {
        "pycharm": {
          "metadata": false,
          "name": "#%%\n"
        }
      }
    },
    {
      "cell_type": "markdown",
      "source": "* 12 导入数字和文本数据集 保持文本在numpy数组中完好",
      "metadata": {
        "pycharm": {
          "metadata": false
        }
      }
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "outputs": [],
      "source": "# Solution\nurl \u003d \u0027https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data\u0027\niris \u003d np.genfromtxt(url, delimiter\u003d\u0027,\u0027, dtype\u003d\u0027object\u0027)\nnames \u003d (\u0027sepallength\u0027, \u0027sepalwidth\u0027, \u0027petallength\u0027, \u0027petalwidth\u0027, \u0027species\u0027)\n# Print the first 3 rows\niris[:5]\n",
      "metadata": {
        "pycharm": {
          "metadata": false,
          "name": "#%% \n"
        }
      }
    },
    {
      "cell_type": "markdown",
      "source": "* 13 将一维元组数组 转换为2维numpy数组\n* *  通过省略鸢尾属植物数据集种类的文本字段，将一维鸢尾属植物数据集转换为二维数组iris_2d",
      "metadata": {
        "pycharm": {
          "metadata": false
        }
      }
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "outputs": [],
      "source": "url \u003d \u0027https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data\u0027\niris_1d \u003d np.genfromtxt(url, delimiter\u003d\u0027,\u0027, dtype\u003dNone)\n\niris_2d \u003d np.genfromtxt(url, delimiter\u003d\u0027,\u0027, dtype\u003d\u0027float\u0027, usecols\u003d[0, 1, 2, 3])\niris_2d[: 5]\n",
      "metadata": {
        "pycharm": {
          "metadata": false,
          "name": "#%%\n"
        }
      }
    },
    {
      "cell_type": "markdown",
      "source": "* 14 计算numpy数组中的 均值 中位数 标注差\n* * 求出鸢尾属植物萼片 长度 的平均值、中位数和标准差(第1列)\n* * 均值:mean 中位数:media 标准差:std",
      "metadata": {
        "pycharm": {
          "metadata": false
        }
      }
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "outputs": [],
      "source": "url \u003d \u0027https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data\u0027\niris \u003d np.genfromtxt(url, delimiter\u003d\u0027,\u0027, dtype\u003d\u0027object\u0027)\nsepallength \u003d np.genfromtxt(url, delimiter\u003d\u0027,\u0027, dtype\u003d\u0027float\u0027, usecols\u003d[0])\nmean, median, std \u003d np.mean(sepallength), np.median(sepallength), np.std(sepallength)\nprint(mean, median, std)\n",
      "metadata": {
        "pycharm": {
          "metadata": false,
          "name": "#%%\n"
        }
      }
    },
    {
      "cell_type": "markdown",
      "source": "* 15 归一化数组 利于查看数组分布\n* * 创建一种标准化形式的鸢尾属植物间隔长度,值位于0-1之间,最小值0,最大值1",
      "metadata": {
        "pycharm": {
          "metadata": false
        }
      }
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "outputs": [],
      "source": "import seaborn\nimport numpy as np\nfrom scipy import stats\nurl \u003d \u0027https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data\u0027\nsepallength \u003d np.genfromtxt(url, delimiter\u003d\u0027,\u0027, dtype\u003d\u0027float\u0027, usecols\u003d[0])\nsmax, smin \u003d sepallength.max(), sepallength.min()\ns \u003d (sepallength - smin) / (smax - smin) # (当前值-最大值) / (标准差)\nseaborn.set(color_codes\u003dTrue)  # 使用默认颜色编码\nseaborn.distplot(s, bins\u003d20, fit\u003dstats.gamma, color\u003d\u0027green\u0027 ) # 显示 gama, 以及高斯分布线\nprint(s)\n\n",
      "metadata": {
        "pycharm": {
          "metadata": false,
          "name": "#%%\n"
        }
      }
    },
    {
      "cell_type": "markdown",
      "source": "* 16 计算 sepalleng 的 softmax 分数\n* * 分类的问题，简单的讲：分类就是设定一个阈值，将你想分类的对象与这个阈值进行比较，根据结果来进行决定分类\n* * softmax函数：本质是将一个K维的任意实数向量压缩（映射）成另一个K维的实数向量\n* * 其中向量中的每个元素取值都介于（0，1）之间。 \n* * 与logistic函数不同的是，logistic函数主要解决的是二分类问题\n* * 而softmax函数主要解决的是多分类问题 \n* * 计算sepallength的softmax分数。",
      "metadata": {
        "pycharm": {
          "metadata": false
        }
      }
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "outputs": [],
      "source": "url \u003d \u0027https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data\u0027\nsepallength \u003d np.genfromtxt(url, delimiter\u003d\u0027,\u0027, dtype\u003d\u0027float\u0027, usecols\u003d[0])\n\n# softmax函数实现\ndef softmax(x):\n    \"\"\"Compute softmax values for each sets of scores in x.\"\"\"\n    e_x \u003d np.exp(x - np.max(x))\n    return e_x / e_x.sum(axis\u003d0)\nprint(softmax(sepallength))\n",
      "metadata": {
        "pycharm": {
          "metadata": false,
          "name": "#%%\n"
        }
      }
    },
    {
      "cell_type": "markdown",
      "source": "* 17 找到numpy数组的百分位数据\n* * 使用.percentle 方法\n* * 找到数据集的第5 百分位, 和第95 百分位 ",
      "metadata": {
        "pycharm": {
          "metadata": false
        }
      }
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "outputs": [],
      "source": "url \u003d \u0027https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data\u0027\nsepallength \u003d np.genfromtxt(url, delimiter\u003d\u0027,\u0027, dtype\u003d\u0027float\u0027, usecols\u003d[0])\n\n# Solution\nnp.percentile(sepallength, q\u003d[5, 95])\n",
      "metadata": {
        "pycharm": {
          "metadata": false,
          "name": "#%% \n"
        }
      }
    },
    {
      "cell_type": "markdown",
      "source": "* 18 在数组中的随机位置插入值\n* * 在iris数据集中20个随机位置插入 np.nan 空值",
      "metadata": {
        "pycharm": {
          "metadata": false
        }
      }
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "outputs": [],
      "source": "# Input\nurl \u003d \u0027https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data\u0027\niris_2d \u003d np.genfromtxt(url, delimiter\u003d\u0027,\u0027, dtype\u003d\u0027object\u0027)\n\n# Method 2\nnp.random.seed(100)\nnp.random.randint(150, size\u003d20) # 生成20个150以内的随机值 作为行标\nnp.random.randint(4, size\u003d20)  # 生成20个4以内的随机值 作为列标\niris_2d[np.random.randint(150, size\u003d20), np.random.randint(4, size\u003d20)] \u003d np.nan\n# 利用随机生成的 行标 列标 作为索引 用 np.nan 进行填充\n# Print first 10 rows \nprint(iris_2d[:10]) # 输出前10行\n",
      "metadata": {
        "pycharm": {
          "metadata": false,
          "name": "#%%\n"
        }
      }
    },
    {
      "cell_type": "markdown",
      "source": "* 19 找到numpy数组中的缺失值的位置\n* * .isnan 判断缺失值\n* * .where 找到位置\n* * .sum 统计缺失值个数",
      "metadata": {
        "pycharm": {
          "metadata": false
        }
      }
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "outputs": [],
      "source": "# 数据源 在iris_2d 中 随机生成20个缺失值 np.nan\nurl \u003d \u0027https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data\u0027\niris_2d \u003d np.genfromtxt(url, delimiter\u003d\u0027,\u0027, dtype\u003d\u0027float\u0027, usecols\u003d[0,1,2,3])\niris_2d[np.random.randint(150, size\u003d20), np.random.randint(4, size\u003d20)] \u003d np.nan\n\nprint(\"缺失值个数\", np.isnan(iris_2d[:, 0]).sum()) # 第一列的所有缺失值个数\nprint(\"缺失值位置\", np.where(np.isnan(iris_2d[:, 0]))) # 第一列的所有的缺失值的个数\n",
      "metadata": {
        "pycharm": {
          "metadata": false,
          "name": "#%%\n"
        }
      }
    },
    {
      "cell_type": "markdown",
      "source": "* 20 根据两个或者多个条件 过滤numpy数组\n* * 筛选出具有特殊特征的记录\n* * 过滤出 第三列属性 \u003e 1.5 和第一列属性 \u003c 5.0 的iris_2d数据的所有行",
      "metadata": {
        "pycharm": {
          "metadata": false
        }
      }
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "outputs": [],
      "source": "url \u003d \u0027https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data\u0027\niris_2d \u003d np.genfromtxt(url, delimiter\u003d\u0027,\u0027, dtype\u003d\u0027float\u0027, usecols\u003d[0,1,2,3]) # 选出第 1,2,3,4列的数据\n\nconditions \u003d (iris_2d[:, 0] \u003c 5.0) \u0026 (iris_2d[:, 2] \u003e1.5)\niris_2d[conditions]\n",
      "metadata": {
        "pycharm": {
          "metadata": false,
          "name": "#%% \n"
        }
      }
    },
    {
      "cell_type": "markdown",
      "source": "* 21 删除numpy数组中包含缺失值的行(记录)\n* * 选择出没有任何空值的记录(行)",
      "metadata": {
        "pycharm": {
          "metadata": false
        }
      }
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "outputs": [],
      "source": "# 生成具有空值值的数据\nurl \u003d \u0027https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data\u0027\niris_2d \u003d np.genfromtxt(url, delimiter\u003d\u0027,\u0027, dtype\u003d\u0027float\u0027, usecols\u003d[0,1,2,3])\niris_2d[np.random.randint(150, size\u003d20), np.random.randint(4, size\u003d20)] \u003d np.nan\n\n# 筛选出没有空值的记录\nnon_nan_in_row \u003d np.array([~np.any(np.isnan(row)) for row in iris_2d])\nprint(iris_2d[:5])\niris_2d[non_nan_in_row][:5]\n",
      "metadata": {
        "pycharm": {
          "metadata": false,
          "name": "#%% \n"
        }
      }
    },
    {
      "cell_type": "markdown",
      "source": "* 22 找到numpy数组两列之间的相关性\n* * 使用Pearson 相关系数来衡量\n* * Pearson 相关系数是用协方差除以两个变量的标准差得到的，\n* * 虽然协方差能反映两个随机变量的相关程度（协方差大于0的时候表示两者正相关，小于0的时候表示两者负相关），\n* * 但其数值上受量纲的影响很大，不能简单地从协方差的数值大小给出变量相关程度的判断。\n* * 为了消除这种量纲的影响，于是就有了相关系数的概念\n* * 在iris_2d中找出SepalLength（第1列）和PetalLength（第3列）之间的相关性",
      "metadata": {
        "pycharm": {
          "metadata": false
        }
      }
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "outputs": [],
      "source": "url \u003d \u0027https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data\u0027\niris \u003d np.genfromtxt(url, delimiter\u003d\u0027,\u0027, dtype\u003d\u0027float\u0027, usecols\u003d[0,1,2,3])\n\n# 方法1 \nprint(np.corrcoef(iris[:, 0], iris[:, 2])[0, 1])\n# Solution 2\nfrom scipy.stats.stats import pearsonr  \ncorr, p_value \u003d pearsonr(iris[:, 0], iris[:, 2])\nprint(corr)\n",
      "metadata": {
        "pycharm": {
          "metadata": false,
          "name": "#%%\n"
        }
      }
    },
    {
      "cell_type": "markdown",
      "source": "* 23 查看数组是否存在空值\n* * np.any()",
      "metadata": {
        "pycharm": {
          "metadata": false
        }
      }
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "outputs": [],
      "source": "url \u003d \u0027https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data\u0027\niris_2d \u003d np.genfromtxt(url, delimiter\u003d\u0027,\u0027, dtype\u003d\u0027float\u0027, usecols\u003d[0,1,2,3])\n\nnp.isnan(iris_2d).any()\n",
      "metadata": {
        "pycharm": {
          "metadata": false,
          "name": "#%%\n"
        }
      }
    },
    {
      "cell_type": "markdown",
      "source": "* 24 用0替换数组中的空值",
      "metadata": {
        "pycharm": {
          "metadata": false
        }
      }
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "outputs": [],
      "source": "url \u003d \u0027https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data\u0027\niris_2d \u003d np.genfromtxt(url, delimiter\u003d\u0027,\u0027, dtype\u003d\u0027float\u0027, usecols\u003d[0,1,2,3])\niris_2d[np.random.randint(150, size\u003d20), np.random.randint(4, size\u003d20)] \u003d np.nan\n\niris_2d[np.isnan(iris_2d)] \u003d 0\niris_2d[:10]\n",
      "metadata": {
        "pycharm": {
          "metadata": false,
          "name": "#%%\n"
        }
      }
    },
    {
      "cell_type": "markdown",
      "source": "* 25 找出数组中的独特值以及独特值的数量",
      "metadata": {
        "pycharm": {
          "metadata": false
        }
      }
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "outputs": [],
      "source": "",
      "metadata": {
        "pycharm": {
          "metadata": false,
          "name": "#%%\n"
        }
      }
    },
    {
      "cell_type": "markdown",
      "source": "* 26 将数组中的记录的属性值中的数值转换为为文本\n* * 将iris_2d的花瓣长度（第3列）加入以形成文本数组，这样如果花瓣长度为：\n* * Less than 3 --\u003e \u0027small\u0027 3-5 --\u003e \u0027medium\u0027 \u0027\u003e\u003d5 --\u003e \u0027large\u0027\n",
      "metadata": {
        "pycharm": {
          "metadata": false
        }
      }
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "outputs": [],
      "source": "url \u003d \u0027https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data\u0027\niris \u003d np.genfromtxt(url, delimiter\u003d\u0027,\u0027, dtype\u003d\u0027object\u0027)\nnames \u003d (\u0027sepallength\u0027, \u0027sepalwidth\u0027, \u0027petallength\u0027, \u0027petalwidth\u0027, \u0027species\u0027)\n\n# Bin petallength \npetal_length_bin \u003d np.digitize(iris[:, 2].astype(\u0027float\u0027), [0, 3, 5, 10])\nprint(petal_length_bin)\n# Map it to respective category\nlabel_map \u003d {1: \u0027small\u0027, 2: \u0027medium\u0027, 3: \u0027large\u0027, 4: np.nan}\nprint(label_map)\npetal_length_cat \u003d [label_map[x] for x in petal_length_bin]\nprint(petal_length_cat)\n# View\npetal_length_cat[:4]\n# [\u0027small\u0027, \u0027small\u0027, \u0027small\u0027, \u0027small\u0027]\n",
      "metadata": {
        "pycharm": {
          "metadata": false,
          "name": "#%%\n"
        }
      }
    },
    {
      "cell_type": "markdown",
      "source": "* 27 在数组中使用属性值以及计算创建新的属性值字段\n* * 1 取字段\n* * 2 计算\n* * 3 插入新的字段",
      "metadata": {
        "pycharm": {
          "metadata": false
        }
      }
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "outputs": [],
      "source": "",
      "metadata": {
        "pycharm": {
          "metadata": false,
          "name": "#%%\n"
        }
      }
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "outputs": [],
      "source": "# Input\nurl \u003d \u0027https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data\u0027\niris_2d \u003d np.genfromtxt(url, delimiter\u003d\u0027,\u0027, dtype\u003d\u0027object\u0027)\n\n# Solution\n# 取出字段\nsepallength \u003d iris_2d[:, 0].astype(\u0027float\u0027)\npetallength \u003d iris_2d[:, 2].astype(\u0027float\u0027)\n# 计算新的字段值\nvolume \u003d (np.pi * petallength * (sepallength**2))/3\n\n# 插入新的字段\nvolume \u003d volume[:, np.newaxis] \n\n# 字段插入源表\nout \u003d np.hstack([iris_2d, volume])\n\n# View\nout[:4]\n",
      "metadata": {
        "pycharm": {
          "metadata": false,
          "name": "#%%\n"
        }
      }
    },
    {
      "cell_type": "markdown",
      "source": "* 28 numpy数组中进行概率抽样",
      "metadata": {
        "pycharm": {
          "metadata": false
        }
      }
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "outputs": [],
      "source": "# Import iris keeping the text column intact\nurl \u003d \u0027https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data\u0027\niris \u003d np.genfromtxt(url, delimiter\u003d\u0027,\u0027, dtype\u003d\u0027object\u0027)\n\n# Solution\n# Get the species column\nspecies \u003d iris[:, 4]\n\n# Approach 1: Generate Probablistically\nnp.random.seed(100)\na \u003d np.array([\u0027Iris-setosa\u0027, \u0027Iris-versicolor\u0027, \u0027Iris-virginica\u0027])\nspecies_out \u003d np.random.choice(a, 150, p\u003d[0.5, 0.25, 0.25])\n\n# Approach 2: Probablistic Sampling (preferred)\nnp.random.seed(100)\nprobs \u003d np.r_[np.linspace(0, 0.500, num\u003d50), np.linspace(0.501, .750, num\u003d50), np.linspace(.751, 1.0, num\u003d50)]\nindex \u003d np.searchsorted(probs, np.random.random(150))\nspecies_out \u003d species[index]\nprint(np.unique(species_out, return_counts\u003dTrue))\n",
      "metadata": {
        "pycharm": {
          "metadata": false,
          "name": "#%%\n"
        }
      }
    },
    {
      "cell_type": "markdown",
      "source": "* 29 按列对二维数组排序",
      "metadata": {
        "pycharm": {
          "metadata": false
        }
      }
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "outputs": [],
      "source": "url \u003d \u0027https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data\u0027\niris \u003d np.genfromtxt(url, delimiter\u003d\u0027,\u0027, dtype\u003d\u0027object\u0027)\nnames \u003d (\u0027sepallength\u0027, \u0027sepalwidth\u0027, \u0027petallength\u0027, \u0027petalwidth\u0027, \u0027species\u0027)\n\nprint(iris[iris[:, 0].argsort()][:20])\n",
      "metadata": {
        "pycharm": {
          "metadata": false,
          "name": "#%%\n"
        }
      }
    },
    {
      "cell_type": "markdown",
      "source": "* 30 将pil图像转换为数组",
      "metadata": {
        "pycharm": {
          "metadata": false
        }
      }
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "outputs": [],
      "source": "from io import BytesIO\nfrom PIL import Image\nimport PIL, requests\n\n# Import image from URL\nURL \u003d \u0027https://upload.wikimedia.org/wikipedia/commons/8/8b/Denali_Mt_McKinley.jpg\u0027\nresponse \u003d requests.get(URL)\n\n# Read it as Image\nI \u003d Image.open(BytesIO(response.content))\n\n# Optionally resize\nI \u003d I.resize([150,150])\n\n# Convert to numpy array\narr \u003d np.asarray(I)\n\n# Optionaly Convert it back to an image and show\nim \u003d PIL.Image.fromarray(np.uint8(arr))\nImage.Image.show(im)\n",
      "metadata": {
        "pycharm": {
          "metadata": false,
          "name": "#%%\n"
        }
      }
    },
    {
      "cell_type": "markdown",
      "source": "* 31 计算数组之间的欧式距离",
      "metadata": {
        "pycharm": {
          "metadata": false
        }
      }
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "outputs": [],
      "source": "a \u003d np.array([1,2,3,4,5])\nb \u003d np.array([4,5,6,7,8])\n\n# Solution\ndist \u003d np.linalg.norm(a-b)\ndist\n",
      "metadata": {
        "pycharm": {
          "metadata": false,
          "name": "#%%\n"
        }
      }
    },
    {
      "cell_type": "markdown",
      "source": "* 32 找到一位数组中的所有峰值, 峰值两边被较小的值包围",
      "metadata": {
        "pycharm": {
          "metadata": false
        }
      }
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "outputs": [],
      "source": "a \u003d np.array([1, 3, 7, 1, 2, 6, 0, 1])\ndoublediff \u003d np.diff(np.sign(np.diff(a)))\npeak_locations \u003d np.where(doublediff \u003d\u003d -2)[0] + 1\npeak_locations\n",
      "metadata": {
        "pycharm": {
          "metadata": false,
          "name": "#%%\n"
        }
      }
    },
    {
      "cell_type": "markdown",
      "source": "* 33 填写不规则系列的numpy日期中的缺失日期\n*  * 给定一系列不连续的日期序列。填写缺失的日期，使其成为连续的日期序列",
      "metadata": {
        "pycharm": {
          "metadata": false
        }
      }
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "outputs": [],
      "source": "dates \u003d np.arange(np.datetime64(\u00272018-02-01\u0027), np.datetime64(\u00272018-02-25\u0027), 2)\nprint(dates)\n\n# Solution ---------------\nfilled_in \u003d np.array([np.arange(date, (date+d)) for date, d in zip(dates, np.diff(dates))]).reshape(-1)\n\n# add the last day\noutput \u003d np.hstack([filled_in, dates[-1]])\noutput\n\n# For loop version -------\nout \u003d []\nfor date, d in zip(dates, np.diff(dates)):\n    out.append(np.arange(date, (date+d)))\n\nfilled_in \u003d np.array(out).reshape(-1)\n\n# add the last day\noutput \u003d np.hstack([filled_in, dates[-1]])\noutput",
      "metadata": {
        "pycharm": {
          "metadata": false,
          "name": "#%%\n"
        }
      }
    }
  ],
  "metadata": {
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 2
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython2",
      "version": "2.7.6"
    },
    "kernelspec": {
      "name": "python3",
      "language": "python",
      "display_name": "Python 3"
    },
    "stem_cell": {
      "cell_type": "raw",
      "source": "",
      "metadata": {
        "pycharm": {
          "metadata": false
        }
      }
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}